package com.ligongmusic.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ligongmusic.R;
import com.ligongmusic.adapter.UniversalAdapter;
import com.ligongmusic.adapter.ViewHolder;
import com.ligongmusic.domain.MyComments;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.util.JsonUtils;
import com.ligongmusic.util.LoggerUtils;
import com.ligongmusic.util.NetWorkUtils;
import com.ligongmusic.util.NetWorkUtils.NetWorkCallBack;
import com.ligongmusic.util.StringUtils;
import com.ligongmusic.view.RoundImageView;
import com.ligongmusic.view.TopManager;
import com.ligongmusic.view.XListView;
import com.ligongmusic.view.XListView.IXListViewListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;


public class MyComment extends Activity implements IXListViewListener, NetWorkCallBack {
	private XListView mListView;
	private Handler mHandler = new Handler();
	private boolean hasData = true;
	private UniversalAdapter<MyComments> adapter = null;
	private List<MyComments> comments;
	private String nickName;
	private int page = 0, size = 10;
	public static DisplayImageOptions options1 = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.head) //设置图片在下载期间显示的图片
			.showImageForEmptyUri(R.drawable.head)//设置图片Uri为空或是错误的时候显示的图片
			.showImageOnFail(R.drawable.head)  //设置图片加载/解码过程中错误时候显示的图片
			.cacheInMemory(true)//设置下载的图片是否缓存在内存中
			.imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
			.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型
			.build();//构建完成;
	public static DisplayImageOptions options2 = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.music_icon) //设置图片在下载期间显示的图片
			.showImageForEmptyUri(R.drawable.music_icon)//设置图片Uri为空或是错误的时候显示的图片
			.showImageOnFail(R.drawable.music_icon)  //设置图片加载/解码过程中错误时候显示的图片
			.cacheInMemory(true)//设置下载的图片是否缓存在内存中
			.imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
			.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型
			.build();//构建完成;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_comment);
		nickName = getIntent().getStringExtra("nickName");
		TopManager.getTopManager(this).showTop(true, "我的评论", new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mListView = (XListView) findViewById(R.id.comment_list);
		mListView.setXListViewListener(this);
		getCommentList();
	}

	/**
	 * 获取我的评论列表
	 */
	private void getCommentList() {
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("loginname", GlobalParams.userName));
		pairs.add(new BasicNameValuePair("token", GlobalParams.token));
		pairs.add(new BasicNameValuePair("page", page + ""));
		pairs.add(new BasicNameValuePair("size", size + ""));
		NetWorkUtils.getRequest(pairs, GlobalParams.MY_COMMENT, this);
	}

	@Override
	public void success(String result) {
		LoggerUtils.getLog(MyComment.class).error(result);
		onLoad();
		if (comments == null)
			comments = new ArrayList<MyComments>();
		if (page == 0)
			comments.clear();
		List<MyComments> temp = JsonUtils.jsonToBeanList(result, MyComments.class);
		if (temp == null)
			return;
		mListView.setPullLoadEnable(temp.size() == size);
		hasData = temp.size() == size;
		for (MyComments u : temp)
			comments.add(u);
		setAdapter();
	}

	/**
	 * 设置数据适配器
	 */
	private void setAdapter() {
		if (adapter == null) {
			adapter = new UniversalAdapter<MyComments>(MyComment.this, comments, R.layout.my_comments_item) {
				@Override
				public void convert(ViewHolder holder, MyComments t, int position) {
					ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + t.getCover(), (ImageView) holder.getView(R.id.music_icon), options2);
					ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + t.getPhoto(), (RoundImageView) holder.getView(R.id.user_head), options1);
					// 设置字体大小、颜色
					String nick_name = t.getNick_name();
					if (StringUtils.isEmpty(nick_name))
						nick_name= t.getPhone();
					SpannableStringBuilder shareBuilder = new SpannableStringBuilder(nick_name + "上传了单曲");
					AbsoluteSizeSpan shareSize = new AbsoluteSizeSpan(30);
					ForegroundColorSpan shareSpan = new ForegroundColorSpan(getResources().getColor(R.color.color_red));
					shareBuilder.setSpan(shareSpan, 0, nick_name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					shareBuilder.setSpan(shareSize, 0, nick_name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					TextView shareInfo = holder.getView(R.id.tv_share_info);
					shareInfo.setText(shareBuilder);
					holder.setText(R.id.tv_upload_date, StringUtils.getYear(t.getDate()));
					holder.setText(R.id.tv_music_name, t.getSong_name());
					holder.setText(R.id.tv_singer, t.getMusician_name() + "");

					SpannableStringBuilder builder = new SpannableStringBuilder(nickName + "： " + t.getContent());
					ForegroundColorSpan span = new ForegroundColorSpan(getResources().getColor(R.color.color_red));
					builder.setSpan(span, 0, nickName.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					TextView commentContent = holder.getView(R.id.comment_content);
					commentContent.setText(builder);
				}
			};
			mListView.setAdapter(adapter);
		} else
			adapter.refresh(comments);
	}

	@Override
	public void error() {
		// TODO Auto-generated method stub
	}

	@Override
	public void error(String message) {
		LoggerUtils.getLog(MyComment.class).error(message);
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				hasData = true;
				page = 0;
				getCommentList();
			}
		}, 1000);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime(StringUtils.getTime(System.currentTimeMillis()));
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (hasData) {
					page++;
					getCommentList();
				} else {
					onLoad();
				}
			}
		}, 1000);
	}

}
